<template>
  <div class="empadd-page">
    <el-dialog title="新增员工" :visible="dialogFormVisible" @close="closeBtn">
      <el-form
        :model="form"
        style="width: 75%; margin: 0 auto"
        :rules="rules"
        ref="addForm"
      >
        <el-form-item
          label="姓名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input
            v-model="form.username"
            autocomplete="off"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="手机号"
          :label-width="formLabelWidth"
          prop="mobile"
        >
          <el-input
            v-model="form.mobile"
            autocomplete="off"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="入职时间"
          :label-width="formLabelWidth"
          prop="timeOfEntry"
        >
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="请选择入职时间"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="聘用形式"
          :label-width="formLabelWidth"
          prop="formOfEmployment"
        >
          <el-select
            v-model="form.formOfEmployment"
            placeholder="请选择聘用形式"
            style="width: 100%"
          >
            <el-option
              v-for="item in hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="工号"
          :label-width="formLabelWidth"
          prop="workNumber"
        >
          <el-input
            v-model="form.workNumber"
            autocomplete="off"
            placeholder="请输入工号"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="部门"
          :label-width="formLabelWidth"
          prop="departmentName"
        >
          <el-input
            v-model="form.departmentName"
            autocomplete="off"
            placeholder="请选择部门"
            @focus="getList"
            @blur="handleBlur"
          ></el-input>
          <!-- data:展示数据
               props =defaultProps默认
               @node-click节点被点击时的回调-->
          <el-tree
            v-if="showTree"
            v-loading="isLoading"
            :data="data"
            :props="defaultProps"
            @node-click="nodeClick"
            default-expand-all
          ></el-tree>
        </el-form-item>
        <el-form-item
          label="转正时间"
          :label-width="formLabelWidth"
          prop="correctionTime"
        >
          <!-- 由于时间是时间选择器,所以不能使用失去焦点(blur),而是要使用change事件. -->
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="请选择入职时间"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeBtn">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import constant from "@/api/constant/employees";
import { addEmp } from "@/api/employees";
//导入部门列表的接口
import { getDepartments } from "@/api/departments";
//导入数据转树
import { arrayToTree } from "@/utils/index";
export default {
  name: "addDepts",
  props: {
    dialogFormVisible: {
      type: Boolean,
      require: true,
    },
    formInfo: {
      type: Object,
    },
  },
  data() {
    return {
      //加载的动画效果,默认关闭
      isLoading: false,
      //是否显示树结构
      showTree: false,
      //聘用形式做判断
      hireType: constant.hireType,
      //tree的数据
      data: [],
      // 指定树的文字和子节点是什么属性
      defaultProps: {
        label: "name",
        children: "children",
      },
      //新增表单数据
      form: {
        username: "",
        mobile: "",
        timeOfEntry: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        correctionTime: "",
      },
      //label的宽度
      formLabelWidth: "80px",
      //弹出框是否显示
      //   dialogFormVisible: dialogFormVisible,
      //表单规则
      rules: {
        username: [
          { required: true, message: "请输入员工姓名", trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入员工手机号", trigger: "blur" },
        ],
        timeOfEntry: [
          { required: true, message: "请选择入职时间", trigger: "change" },
        ],
        formOfEmployment: [
          { required: true, message: "请选择聘用形式", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "请输入工号", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "请选择部门", trigger: "change" },
        ],
        correctionTime: [
          { required: true, message: "请选择转正时间", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    //点击确认新增员工
    async save() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          await addEmp(this.form);
          this.$message.success("新增成功");
          this.formInfo.total += 1;
          let page = this.formInfo.total / this.formInfo.size;
          this.formInfo.page = Math.ceil(page);
          this.$emit("formInfo", this.formInfo.page);
          //重新获取列表第一种方法
          this.$emit("getEmployeesList");
          //重新获取列表的第二种方法,这个也是可以的,但是如果父组件太多就不能使用
          //   this.$parent.getEmployeesList()
          this.$refs.addForm.resetFields();
          this.$emit("update:dialogFormVisible", false);
        }
      });
    },
    //关闭窗口
    closeBtn() {
      //清空表单数据
      this.form = {
        username: "",
        mobile: "",
        timeOfEntry: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        correctionTime: "",
      };
      //重置表单的数据
      this.$refs.addForm.resetFields();
      //关闭弹出框
      this.this.$emit("update:dialogFormVisible", false);
    },
    //获得焦点的时候,请求部门列表
    async getList() {
      this.isLoading = true;
      this.showTree = true;
      let res = await getDepartments();
      let list = res.data.depts;
      //调用封装好的数组转tree,然后赋值给tree
      this.data = arrayToTree(list, "");
      this.isLoading = false;
    },
    // 参数1：data:被点的这个节点的数据
    // 参数2: node:被点的这个节点对象
    // 参数3: tree:被点的这个节点所在的tree组件对象
    nodeClick(data, node, tree) {
      // 把被点的节点数据里的name赋值给文本框绑定的数据
      this.form.departmentName = data.name;
      //获取到数据之后关闭tree
      this.showTree = false;
    },
    //如果只是点击选择部门,但是没有选择,这个时候我们失去焦点的时候需要将tree隐藏
    handleBlur(e) {
      //   console.log(e);
      // 如果失去焦点时去的不是tree，则隐藏tree
      if (e.relatedTarget.className.indexOf("el-tree-node") == -1) {
        this.showTree = false;
      }
    },
  },
};
</script>

<style lang='scss'>
.el-dialog__header {
  background-color: #66b1ff;
  .el-dialog__title {
    color: #fff;
  }
  .el-dialog__close::before {
    color: #fff;
  }
}
</style>